package com.dman.multi.cmd;

import java.awt.Desktop;
import java.io.File;
import java.io.IOException;

import org.apache.pivot.collections.List;
import org.apache.pivot.wtk.Prompt;

import com.dman.multi.core.Download;
import com.dman.multi.ui.DMan;

public class GUIOpenFolderCommand implements Command {
	
	private DMan parent;
	private List<Download> downloadList;
	
	public GUIOpenFolderCommand(DMan parent, List<Download> downloadList) {
		this.parent = parent;
		this.downloadList = downloadList;
	}

	@Override
	public void execute() {
		int index = parent.getSelectedTableViewIndex();
		if(index >= 0){
			Download d = downloadList.get(index);
			boolean canOpen = false;
			try{
				if(Desktop.isDesktopSupported()){
					Desktop desktop = Desktop.getDesktop();
					if(desktop.isSupported(Desktop.Action.OPEN)){
						File f = new File(d.getDirName());
						desktop.open(f);
						canOpen = true;
					}
				}
			} catch (IOException e) {}
			
			if(!canOpen)
				Prompt.prompt("Unable to open folder for selected download", parent);
		}
	}

}
